package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constants.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

public class OrderAction extends BaseAction<Order> {
	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_save", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String saveOrder() {
		// 寄件人区域
		Area sendArea = new Area();
		splitArea(sendAreaInfo, sendArea);
		model.setSendArea(sendArea);
		// 收件人区域
		Area recArea = new Area();
		splitArea(recAreaInfo, recArea);
		model.setRecArea(recArea);
		// 判断用户是否已经登录
		Customer customer = (Customer) ActionContext.getContext().getSession().get("customer");
		System.err.println(customer);
		if (customer != null) {
			model.setCustomer_id(customer.getId());
		}
		//通过webService保存订单
		WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/orderService/order").type(MediaType.APPLICATION_JSON)
		.post(model);
		return SUCCESS;
	}

	// 切割详细信息的公共方法
	public void splitArea(String info, Area area) {
		String[] str = info.split("/");
		area.setProvince(str[0]);
		area.setCity(str[1]);
		area.setDistrict(str[2]);
	}
}
